<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of user_model
 *
 * @author souzadavi
 */
class User_model extends CI_Model {

    //put your code here
    function __construct() {
        parent::__construct();
        $this->load->config('user/parametros');
    }

    function lista_usuarios() {
        $this->db->select($this->config->item('tblUsers') . '.*, ' . $this->config->item('tblUsersType') . '.department');
        $this->db->join($this->config->item('dbCliente') . $this->config->item('tblUsersType'), $this->config->item('tblUsersType') . '.id = ' . $this->config->item('tblUsers') . '.user_type_id', 'left');
        $query = $this->db->get($this->config->item('dbCliente') . $this->config->item('tblUsers'));
        if ($query->num_rows() > 0) {
            return $query->result();
        } else {
            return NULL;
        }
    }

    function get_usuario($_id) {
        $this->db->where('id', $_id);
        $query = $this->db->get($this->config->item('dbCliente') . $this->config->item('tblUsers'));
        if ($query->num_rows() > 0) {
            return $query->row();
        } else {
            return NULL;
        }
    }

    function alterar_valor_campo($id_registro, $campo, $valor) {
        $this->db->where('id', $id_registro);
        $this->db->set($campo, $valor);
        $this->db->update($this->config->item('dbCliente') . $this->config->item('tblUsers'));
        if ($this->db->affected_rows() > 0) {
            return TRUE;
        } else {
            return FALSE;
        }
    }

    function list_users_type() {
        $query = $this->db->get($this->config->item('dbCliente') . $this->config->item('tblUsersType'));
        if ($query->num_rows() > 0) {
            return $query->result();
        } else {
            return NULL;
        }
    }

    function listar_modulos_status($_modulo_id, $_status = '1') {
        $this->db->select($this->config->item('tblModulos') . '.id as modulos_id, ' .
                $this->config->item('tblModulos') . '.nome, ' .
                $this->config->item('tblUsersRules') . '.id as rules_id, ' .
                $this->config->item('tblUsersRules') . '.function');
        $this->db->where('status', $_status);
        $this->db->where($this->config->item('tblModulos') . '.id', $_modulo_id);
        $this->db->join($this->config->item('dbCliente') . $this->config->item('tblUsersRules'), $this->config->item('tblModulos') . '.id = ' . $this->config->item('tblUsersRules') . '.modulos_id', 'inner');
        $query = $this->db->get($this->config->item('dbCliente') . $this->config->item('tblModulos'));
        if ($query->num_rows() > 0) {
            return $query->result();
        } else {
            return NULL;
        }
    }

    function listar_modulos($_status = '1') {
        $this->db->select($this->config->item('tblModulos') . '.id as modulo_id');
        $this->db->select($this->config->item('tblModulos') . '.nome');
        $this->db->where('status', $_status);
        $query = $this->db->get($this->config->item('dbCliente') . $this->config->item('tblModulos'));
        if ($query->num_rows() > 0) {
            return $query->result();
        } else {
            return NULL;
        }
    }

    function get_permissao($_user_id, $_rule_id) {
        $this->db->select($this->config->item('tblUsersRules') . '.id as rules_id, ' .
                $this->config->item('tblUsersRules') . '.function, ' .
                $this->config->item('tblUsersHasRules') . '.id as has_rules_id');
        $this->db->where('status', '1');
        $this->db->where($this->config->item('tblUsersHasRules') . '.users_id', $_user_id);
        $this->db->where($this->config->item('tblUsersRules') . '.id', $_rule_id);
        $this->db->join($this->config->item('tblUsersRules'), $this->config->item('tblUsersHasRules') . '.users_rules_id = ' . $this->config->item('tblUsersRules') . '.id', 'inner');
        $this->db->join($this->config->item('tblModulos'), $this->config->item('tblUsersRules') . '.modulos_id = ' . $this->config->item('tblModulos') . '.id', 'inner');

        $query = $this->db->get($this->config->item('tblUsersHasRules'));

        if ($query->num_rows() > 0) {
            return $query->row();
        } else {
            return NULL;
        }
    }

    function deletar_permissoes($_modulos_id, $_users_id) {
        $query = $this->db->query("SELECT uhr.id FROM users_has_rules uhr 
                    LEFT JOIN users_rules ur ON uhr.users_rules_id = ur.id 
                 WHERE ur.modulos_id = " . $_modulos_id . " AND uhr.users_id = " . $_users_id);

        if ($query->num_rows() > 0) {
            $buffer = array();

            foreach ($query->result() as $result) {
                $buffer[] = $result->id;
            }

            $this->db->query("delete from users_has_rules WHERE id IN (" . implode(",", $buffer) . ")");
        }
        return false;
    }

    function inserir_permissoes($_array) {
        $this->db->insert($this->config->item('tblUsersHasRules'), $_array);
    }

}
